#!/usr/bin/env groovy

// copy docker tar file to DMZ environmnet, then load docker tar
def call(String techStack = '') {
  echo "======================LOAD DOCKER ON ${DEPLOY_SERVER}(TECHSTACK:${techStack})============================="
     try
     {
        if ( techStack == 'vue') {
           script { 
  	       def remote = [:]
  	       remote.name = 'dmz'
             remote.host = "${DEPLOY_SERVER}"    
             remote.user = "${SERVER_ACCESS_KEY_USR}"  
             remote.allowAnyHosts = true
             withCredentials([sshUserPrivateKey(credentialsId: 'dmz-private-key', keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: '')]) {  
                  echo "sshkey: ${identity}"	                 
                  remote.identityFile = identity
                  sshRemove remote: remote, path: "/home/user/deploy-${PROJECT_NAME}"
                  sshCommand remote: remote, command:"mkdir -p /home/user/deploy-${PROJECT_NAME}"                  
                  sshPut remote: remote, from: "${env.WORKSPACE}/target/${DOCKER_NAME}_${VERSION_NUMBER}.tar", into: "/home/user/deploy-${PROJECT_NAME}"
                  sshCommand remote: remote, command:"docker rm -f ${DOCKER_NAME} > /dev/null 2>&1 && echo 'removed container' || echo 'nothing to remove'"
                  sshCommand remote: remote, command:"docker rmi -f `docker images | grep ${DOCKER_NAME} | awk '{print $3}' ` > /dev/null 2>&1&& echo 'removed images' || echo 'nothing to remove' "
                  sshCommand remote: remote, command:"docker load -i /home/user/deploy-${PROJECT_NAME}/${DOCKER_NAME}_${VERSION_NUMBER}.tar"
                  sshCommand remote: remote, command:"docker run -d --name ${DOCKER_NAME} -p 8088:80 --restart=always ${EXTRA_PARAMETER} ${HARBOR}/${PROJECT_NAME}/${DOCKER_NAME}:${VERSION_NUMBER}"}    }      
    } 
        else if (techStack == 'springboot') {  
           script { 
  	       def remote = [:]
  	       remote.name = 'dmz'
             remote.host = "${DEPLOY_SERVER}"    
             remote.user = "${SERVER_ACCESS_KEY_USR}"  
             remote.allowAnyHosts = true
             withCredentials([sshUserPrivateKey(credentialsId: 'dmz-private-key', keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: '')]) {  
                  echo "sshkey: ${identity}"	                 
                  remote.identityFile = identity
                  sshRemove remote: remote, path: "/home/user/deploy-${PROJECT_NAME}"
                  sshCommand remote: remote, command:"mkdir -p /home/user/deploy-${PROJECT_NAME}"                  
                  sshPut remote: remote, from: "docker-compose.yml", into: "/home/user/deploy-${PROJECT_NAME}"
                  sshPut remote: remote, from: "docker-compose-middleware.yml", into: "/home/user/deploy-${PROJECT_NAME}"
                  sshPut remote: remote, from: "${env.WORKSPACE}/target/${DOCKER_NAME}_${VERSION_NUMBER}.tar", into: "/home/user/deploy-${PROJECT_NAME}"
                  sshCommand remote: remote, command:"docker load -i /home/user/deploy-${PROJECT_NAME}/${DOCKER_NAME}_${VERSION_NUMBER}.tar"

                  sshCommand remote: remote, command:"cd /home/user/deploy-${PROJECT_NAME}; \
                  if [ `docker-compose -f docker-compose-middleware.yml -p middleware ps|grep Up|wc -l` -le 0 ]; then docker-compose -f docker-compose-middleware.yml -p middleware down&&docker-compose -f docker-compose-middleware.yml -p middleware up -d; fi"

                  sshCommand remote: remote, command:"cd /home/user/deploy-${PROJECT_NAME}; \
                  docker-compose -f docker-compose.yml -p ${DOCKER_NAME} ps&&sleep 10; \
                  docker-compose -f docker-compose.yml -p ${DOCKER_NAME} down; \
                  for i in $(cat docker-compose.yml |grep 'image'|cut -d ':' -f 2); do docker rmi -f `docker images|grep $i |awk '{print $3}'` > /dev/null 2>&1&& echo 'removed images' || echo 'nothing to remove'; done ; \
                  TAG=${BUILD_DISPLAY_NAME} docker-compose -f docker-compose.yml -p ${DOCKER_NAME} up -d;"
            }    }                  
                 
        } else{
           throw new RuntimeException("NotImplementedException; ${techStack} IS NOT SUPPORT NOW")    }
 } 
    catch(Exception ex) {
         println("Catching the exception,");
         println(ex.toString());
         println(ex.getMessage());
         println(ex.getStackTrace());  
        //throw
      }
      echo "===================================DONE=====================================" 
}